
#include "Client.h"

#include <iostream>
#include <ostream>
#include <string>
#include <system/System.h>

static std::string host = "127.0.0.1";
static int port         = 4090;

void init_options_description(int argc, char const* argv[]) {
    using namespace program_options;
    program_options::options_description opts;
    opts.add_options()    //
        ("help", "help")  //
        ("host,h",
            value<std::string>(&host)
                ->default_value("127.0.0.1")
                ->implicit_value("127.0.0.1"),
            "server host")  //
        ("port,p",
            value<int>(&port)
                ->default_value(4090)
                ->implicit_value(4090),
            "server port")  //
        ;

    variables_map vm;
    store(parse_command_line(argc, argv, opts), vm);
    notify(vm);
}



void signalHandler(int signal) {
    if (signal == SIGABRT) {
        std::cerr << "abort() has been called!" << std::endl;
        // 在这里可以添加更多的处理逻辑，比如清理资源，记录日志等
        exit(signal); // 确保程序终止
    }
}

int main(int argc, char const* argv[]) { 
    // 注册信号处理器
    // std::signal(SIGABRT, signalHandler);
    signal(SIGABRT, signalHandler);
    try {
        sys::InitConsole();
        init_options_description(argc, argv);

        Client client{host, port};
        client.run();
        std::cout << "exit" << std::endl;
    }
    catch (std::exception& e) {
        std::cout << "error:" << e.what() << std::endl;
    }
    return 0;
}